package com.yimi.yinhepda.db.dao;

import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.yimi.yinhepda.base.BaseApplication;
import com.yimi.yinhepda.db.helper.DbHelper;
import com.yimi.yinhepda.db.table.MoveStockScanMasterTable;

import java.sql.SQLException;
import java.util.List;

/**
 * @author: lj
 * Date: 2019/1/3
 * Description:移库装卸车扫描主表操作
 */
public class MoveStockScanMasterTableDao extends BaseDao<MoveStockScanMasterTable, Long>  {

    public MoveStockScanMasterTableDao() {
        super(DbHelper.getInstance(BaseApplication.getContext()), MoveStockScanMasterTable.class);
    }

    /**
     * 查询移库装卸车数据
     * @param mTaskNo 任务号
     * @param operType 1-装车，2-卸车
     * @return
     */
    public List<MoveStockScanMasterTable> findLoadData(String mTaskNo, int operType) {
        try {
            QueryBuilder<MoveStockScanMasterTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("taskNo", mTaskNo);
            where.and().eq("operType", operType);
            return queryBuilder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据任务号删除
     * @param taskNo 任务号
     * @param operType 1-装车，2-卸车，11-撤销子单（取消装车）
     */
    public void deleteByTaskNo(String taskNo,int operType) {
        List<MoveStockScanMasterTable> list = findLoadData(taskNo,operType);
        try {
            delete(list);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据运单查询
     * @param mTaskNo 任务号
     * @param waybillNo 运单号
     * @return MoveStockScanMasterTable
     */
    public MoveStockScanMasterTable findByWaybillNo(String mTaskNo, String waybillNo) {
        try {
            QueryBuilder<MoveStockScanMasterTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("taskNo", mTaskNo);
            where.and().eq("waybillNo", waybillNo);
            return queryBuilder.queryForFirst();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询强扫的主单
     * @param mTaskNo 任务号
     * @param operType 1-装车，2-卸车
     * @param isForceScan 0-非强扫，1-强扫
     * @return list
     */
    public List<MoveStockScanMasterTable> findUnloadForceScanData(String mTaskNo, int operType, int isForceScan) {
        try {
            QueryBuilder<MoveStockScanMasterTable, Long> queryBuilder = dao.queryBuilder();
            Where where = queryBuilder.where();
            where.eq("taskNo", mTaskNo);
            where.and().eq("operType", operType);
            where.and().eq("isForceScan", isForceScan);
            return queryBuilder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
